const dbUtils = require('../utils/db-util');

// socket具体业务逻辑
module.exports = class socketHander {

	/**
	 * [saveUserSocketId 保存用户的id和socketid]
	 * @param  {[type]} userId   [用户id]
	 * @param  {[type]} socketId [用户的socketid]
	 * @return {[type]}          [description]
	 */
	static async saveUserSocketId(userId, socketId) {
		const sql = 'update user_info set socket_id=? where id=? limit 1 '
		dbUtils.query(sql, [socketId, userId])
	}

	/**
	 * [getUserSocketId 获取指定用户的socketid]
	 * @param  {[type]} toUserId [指定用户的id]
	 * @return {[type]}          [description]
	 */
	static async getUserSocketId(toUserId) {
		const sql = 'select socket_id from user_info where id=? limit 1 '
		const row = await dbUtils.query(sql, [toUserId])
		return row[0].socket_id
	}

	/**
	 * [updateLiveInfo 用户离线，更新直播信息为未开播
	 * @param  {[type]} user_id  [用户id]
	 * @return {[type]}          [description]
	 */
	static async updateLiveInfoAtOffLine(userId) {
		let _sql = `
		    update live_info set is_live=1 where user_id = ${userId}`;
		dbUtils.query(_sql);
	}
}